package leetcode.stack;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/min-stack/submissions/
 *
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

class MinStack {
    private Stack<Integer> stackData;
    private Stack<Integer> stackMin;

    /**
     * initialize your data structure here.
     */
    public MinStack() {
        stackData = new Stack<>();
        stackMin = new Stack<>();
    }

    public void push(int val) {
        if (stackMin.isEmpty() || val <= getMin()) {
            stackMin.push(val);
        }

        stackData.push(val);
    }

    public void pop() {
        if (this.stackData.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        int value = stackData.pop();
        if (value == getMin()) {
            stackMin.pop();
        }
    }

    public int top() {
        if (this.stackData.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return stackData.peek();
    }

    public int getMin() {
        if (this.stackMin.isEmpty()) {
            throw new RuntimeException("stack is empty");
        }
        return stackMin.peek();
    }
}